﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnglishUI.Models;

namespace EnglishUI.Controllers
{
    public class UserController : Controller
    {
        private SQLMVC2008Entities db = new SQLMVC2008Entities();

        //
        // GET: /User/

        public ActionResult Index()
        {
            var tbl_user = db.tbl_user.Include(t => t.tb_Location);
            return View(tbl_user.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(string id = null)
        {
            tbl_user tbl_user = db.tbl_user.Find(id);
            if (tbl_user == null)
            {
                return HttpNotFound();
            }
            return View(tbl_user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            ViewBag.IdCountry = new SelectList(db.tb_Location, "Id", "Country");
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(tbl_user tbl_user)
        {
            if (ModelState.IsValid)
            {
                db.tbl_user.Add(tbl_user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IdCountry = new SelectList(db.tb_Location, "Id", "Country", tbl_user.IdCountry);
            return View(tbl_user);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(string id = null)
        {
            tbl_user tbl_user = db.tbl_user.Find(id);
            if (tbl_user == null)
            {
                return HttpNotFound();
            }
            ViewBag.IdCountry = new SelectList(db.tb_Location, "Id", "Country", tbl_user.IdCountry);
            return View(tbl_user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(tbl_user tbl_user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tbl_user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdCountry = new SelectList(db.tb_Location, "Id", "Country", tbl_user.IdCountry);
            return View(tbl_user);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(string id = null)
        {
            tbl_user tbl_user = db.tbl_user.Find(id);
            if (tbl_user == null)
            {
                return HttpNotFound();
            }
            return View(tbl_user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            tbl_user tbl_user = db.tbl_user.Find(id);
            db.tbl_user.Remove(tbl_user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}